<?php
if(isset($_POST['action']))
{
    $db = new Database;
    // User Info
        $auth = Auth::getAuth('current_user');
        $user_id = $auth['id'];
        $company_id =  $auth['company_id'];
        $user_group = $auth['role'];
        $user_groupType = $auth['role_type'];
        
    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 20; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    // For Sorting
        $dataSortValue = $_POST['dataSortValue'];
        if(!empty($dataSortValue)){
            if($_POST['counterSort']%2==0){
                $sort = "ORDER BY r.date DESC";
            }else{
                $sort = "ORDER BY r.date ASC";
            }
        }else{
            $sort = "ORDER BY rr.approver";
        }
    if($action=="HistoryApproved")
    {
        $getPosition = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where u.id='".$user_id."' ","row");
	$approver = tempRoleApprover($user_groupType)."_".$user_group;
	$approver =  "approver = '".$approver."' or approver = '".$getPosition['position']."' or approver = '".$auth['firstname']." ".$auth['lastname']."'";
	$queryRequest = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0","array");
	$count = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0","numrows");
	
        $no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="search_request"){
	$value = $_POST['value'];
	$getPosition = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where u.id='".$user_id."' ","row");
	$approver = tempRoleApprover($user_groupType)."_".$user_group;
	$approver =  "approver = '".$approver."' or approver = '".$getPosition['position']."' or approver = '".$auth['firstname']." ".$auth['lastname']."'";
	$queryRequest = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0 and status!=4","array");
	$count = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0","numrows");
	
        $no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="searchDate"){
	$value = $_POST['value'];
        $getPosition = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where u.id='".$user_id."' ","row");
	$approver = tempRoleApprover($user_groupType)."_".$user_group;
	$approver =  "approver = '".$approver."' or approver = '".$getPosition['position']."' or approver = '".$auth['firstname']." ".$auth['lastname']."'";
	$queryRequest = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0 and status!=4","array");
	$count = $db->query("SELECT DISTINCT(request_id) FROM tbrequest_process where ($approver) and status!=0","numrows");
	
        $no_of_paginations = ceil($count / $per_page);
    }
    $finaldata.= '<table class="table_data myRequest" style="">
                        <thead>
                            <tr id="sortMyRequest" data-location="tblHistoryRequest" data-search="form_title" data-tag-action="HistoryApproved" p="'.$page.'" data-element="#HistoryApproved">
                                <th>#</th>
				<th>Form Title</th>
                                <th>Requestor</th>';
                                // Get created/ setted form field from the database
                                $field = $db->query("SELECT * FROM tbset_field WHERE companyID='$company_id'","array");
                                $getCreatedfield = $db->query("SELECT * FROM tbset_field WHERE companyID='$company_id'","row");
				$getCreatedfieldRow = $db->query("SELECT * FROM tbset_field WHERE companyID='$company_id'","numrows");
				
                                    $finaldata.= '<th>Processor</th>';
				    $finaldata.= '<th>Action Status</th>';
                                    $finaldata.= '<th>Request Status</th>';
                                
    $finaldata.= '<th>View</th>
			</tr>
                        </thead>
                        <tbody >';
    $counter = 1;
    if($count>0){
        foreach($queryRequest as $dataRequest){
	    $getLastOrPendingApproverState = $db->query("SELECT f.form_title as form_title,
		    rp.approver as approver, rp.approverStatus as approverStatus, r.status as status,
		    CONCAT(u.firstname, u.lastname) as fullname,
		    rp.id as requestProcess_id
		    FROM tbrequest r
		    LEFT JOIN tbrequest_process rp on r.id=rp.request_id
		    LEFT JOIN tbform f on r.form_id=f.id
		    LEFT JOIN tbuser u on r.user_id = u.id
		    WHERE rp.request_id='".$dataRequest['request_id']."' and rp.status!=1","row");
	    $finaldata.='
		<tr id="myRequestRow_'.$dataRequest['request_id'].'" class="historyApprovedRequest_tbl">
		    <td><center>'.$counter++.'</center></td>
		    <td style="text-align:center">'.$getLastOrPendingApproverState['form_title'].'</td>
		    <td style="text-align:center">'.$getLastOrPendingApproverState['fullname'].'</td>';
			
			    
	    $finaldata.='		
		    <td style="text-align:center">'.setApproverTitle($getLastOrPendingApproverState['approver']).'</td>
		    <td style="text-align:center">'.$getLastOrPendingApproverState['approverStatus'].'</td>
		    <td style="text-align:center">
			'.requestStatus($getLastOrPendingApproverState['status']).'
		    </td>
		    <td style="text-align:center">
			<a class="icon-file viewUserForm pointer icon-file" style="color:#6D84B4;font-size:10px" id="viewUserForm_'.$dataRequest['request_id'].'_'.$getLastOrPendingApproverState['requestProcess_id'].'" button-properies="none"></a>
		    </td>
		</tr>';
	}
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
    }else{
        $numCols = $getCreatedfield['numCol'] + 5;
        $finaldata.='<tr><td colspan="'.$numCols.'"><center>No Record Found</center></td></tr>';
        echo $finaldata;
    }
    
}
?>
